from config import msg_info_num , msg_info_speed
from libs.ZNode import ZNode


class ZMessage ( ZNode ) :
    def __init__ ( self , father , rect ) :
        super ( ZMessage , self ).__init__ ( father , rect )
        self.showtime = 4
        self.num = msg_info_num
        self.cache = [ ]
        self.s = 22 / 1000.00
        self.speed = msg_info_speed
        self.bottom = 500

    def show ( self , msg ) :
        self.sort ( )
        if msg._pos [ 1 ] < msg.max_y :
            msg.rect.topleft =  msg.rect.x , msg.rect.y + self.speed
        else :
            if msg.time < self.showtime :
                msg.time += self.s
            else :
                self.destroy ( msg )
        self.surface.blit ( msg.image , msg._pos )

    def destroy ( self , msg ) :
        self.children.remove ( msg )
        if self.cache :
            self.children.append ( self.cache [ 0 ] )
            self.cache.remove ( self.cache [ 0 ] )

    def draw ( self ) :
        if self.running and self.children :
            for x in self.children :
                self.show ( x )

    def add ( self , *msg ) :
        if msg :
            for x in msg :
                x.time = 0
                x.rect.topleft = self._pos
                if len ( self.children ) < self.num and not self.cache :
                    self.children.append ( x )
                else :
                    self.cache.append ( x )

    def sort ( self ) :
        if self.children :
            for id , sur in enumerate ( self.children ) :
                sur.max_y = self.bottom - id * 30

    def update ( self , event , keys , m_keys , m_pos ) :
        pass
